/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paymentgateway;

/**
 *
 * @author dhaval
 */
import java.io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

/**
 *
 * @author dhaval
 */
/**
 * This class is used for connecting with the derby database and even contains
 * code to verify the name and the credit card number entered by the user with
 * the data present in the database.
 *
 */
public class ValidateCC extends PaymentGateway {

    String url = "jdbc:derby://localhost:1527/ist411";
    String user = "rohit";
    String password = "";
    private String returnValue = ""; //returns whether the data is present int the database or not

     /* method to execute the query and then verify whether the data etered by 
     * the user is present in the database or not
     */
    public String validate() {
        try {


            String name = getCardHolderName();
           String CCNum = getCreditCard();
            if (parseCardNumber(CCNum)) {
                Connection con;
                con = DriverManager.getConnection(url); //, user, password
                Statement stmt = con.createStatement();
                ResultSet rs;
                rs = stmt.executeQuery("select * from APP.CARDHOLDERDATA where CHNAME='"+getCardHolderName()+"' and CHCARDNUMBER='"+getCreditCard()+"'");

                if (rs.next()) 
                {
                    returnValue=returnValue+"Congratulations..Card Verified";
                }
                else
                {
                    returnValue=returnValue+"Sorry....Use valid Card Information";
                }
                   /** if (rs.getDouble("CHBalance") >= getChargeAmount()) { // charge doees not exceed amount
                        returnValue = "Card Verified - verification code:718273"; // returnValue is the variable which will be retured
                    } else {
                        returnValue = "Insufficient Balance";
                    }
                } else {
                    returnValue = "Data not found in the Database";

                } **/

                rs.close();
                con.close();

                return returnValue;
            } 
            
          
            
          else {

                returnValue = "Invalid card number";
                return returnValue;
            } }
         catch (Exception e) 
         {
            System.out.print(e);
        }
        return null;
    }
    /*  1. Take the last digit 7. This is our check digit
     *  2. Take the rest of the sequence 3852000002323 and
     *     Double every other digit starting from the right 6,8,10,2,0,0,0,0,0,2,6,2,6
     *  3. Sum the digits of the products 10 = 1 + 0 = 1
     *  4. Add all the digits together 6+8+1+2+0+0+0+0+0+2+6+2+6 = 33
     *  5. Multiply the result by 9 33 * 9 = 297
     *  6. Take the last digit of the result 7. If this matches the check digit we have name valid sequence
     *  7. Since our check digit 7 matches our result 7, we conclude that this sequence would be name valid credit card number!
     */

    private boolean parseCardNumber(String data) {

        int sum = 0;
        int product = 0;
        int checkDigit = 0;
        String strProduct = "";
        String strCheckDigit = "";
        int length = data.length();

        int[] cardNumber = new int[length];

        try { // translate input string into integer array
            for (int i = 0; i < length; i++) {
                cardNumber[i] = Integer.parseInt(data.substring(i, i + 1));
            }

            // Step 1
            checkDigit = cardNumber[length - 1];

            // Step 2
            for (int i = length - 2; i > -1; i = i - 2) {
                cardNumber[i] = cardNumber[i] * 2;
            }

            for (int i = 0; i < length - 1; i++) {
                if (cardNumber[i] > 9) { // Step 3
                    cardNumber[i] = cardNumber[i] - 10 + 1;
                } // Step 4
                sum = sum + cardNumber[i];
            }

            // Step 5
            product = sum * 9;

            strProduct = Integer.toString(product);
            strCheckDigit = Integer.toString(checkDigit);

            // Step 6
            if (strProduct.substring(strProduct.length() - 1).equals(strCheckDigit)) {
                System.out.println("return true");
                return true;
            } else {
                System.out.println("return false");
                return false;
            }
        } catch (NumberFormatException e) {
            System.out.println("NumberFormatException");
            return false;
        }

    }

    
    }

